#include "phpserializetcp.h"
#include "phpserialize.h"
#include "libdserver.h"
#include "dserver_log.h"
#include "dserver_errno.h"

typedef struct _tcp_data {
    long size;
    char data[1];
} tcp_data;

static int php_serialize_on_load(const void *settings, prot_node_t *prot)
{
    settings = 0;
    prot = 0;
    return PROTOCOL_SUCCESS;
}

int protocol_on_load(const void *settings, prot_node_t *prot)
{
    DLOG_ERR(LOG_PROT, "PROTOCOL(TCP): protocol_on_load.");

    if ((settings == NULL) || (prot == NULL)) {
        DLOG_ERR(LOG_PROT, "protocol_on_load parameters error.");
        return -E_ARGS_ILLEGAL;
    }

    return php_serialize_on_load(settings, prot);
}

static int php_serialize_session_init(void *arg)
{
    arg = 0;
    return PROTOCOL_SUCCESS;
}

int protocol_session_init(void *arg)
{
    DLOG_ERR(LOG_PROT, "PROTOCOL(TCP): protocol_session_init.");

    if (arg == NULL) {
        DLOG_ERR(LOG_PROT, "protocol_session_init parameters error.");
        return -E_ARGS_ILLEGAL;
    }

    return php_serialize_session_init(arg);
}

static int php_serialize_session_worker(void *arg)
{
    dlib_php_handle handle = NULL;
    tcp_data *t_data =  NULL;
    int r_size = 0;
    int w_size = 0;

    dserver_conn_t *conn = (dserver_conn_t*)arg;
    if ((conn == NULL) || (conn->c_rsize == 0)) {
        DLOG_ERR(LOG_PROT, "protocol_session_worker received data error.");
        return PROTOCOL_ERROR;
    }

    t_data = (tcp_data*)conn->c_rbuf;
    r_size = ntohl(t_data->size);
    if (r_size <= 0) {
        DLOG_ERR(LOG_PROT, "protocol_session_worker php data error.");
    }

    r_size = 100;
    DLOG_ERR(LOG_PROT, "r_size: %d, r_data: %s", r_size, t_data->data);
    handle = dlib_php_unserialize(t_data->data, r_size);
	DLOG_ERR(LOG_PROT, "unserialize 1.");
    if (handle == NULL) {
	DLOG_ERR(LOG_PROT, "unserialize failed.");
	return PROTOCOL_ERROR;
    }
	DLOG_ERR(LOG_PROT, "unserialize 2.");
    w_size = r_size;
    DLOG_ERR(LOG_PROT, "w_size: %d", w_size);
    conn->c_wsize = dlib_php_serialize(handle, conn->c_wbuf, &w_size);
    conn->c_wbuf[conn->c_wsize] = 0;
    DLOG_ERR(LOG_PROT, "size: %d, result: %s", conn->c_wsize, conn->c_wbuf);
    dlib_php_release_handle(handle);

    dserver_tcp_data_sendout(conn);
    return PROTOCOL_SUCCESS;
}

int protocol_session_worker(void *arg)
{
    DLOG_ERR(LOG_PROT, "PROTOCOL(TCP): protocol_session_worker.");

    if (arg == NULL) {
        DLOG_ERR(LOG_PROT, "protocol_session_worker parameter error.");
        return -E_ARGS_ILLEGAL;
    }

    return php_serialize_session_worker(arg);
}

static int php_serialize_session_exit(void *arg)
{
    arg = 0;
    return PROTOCOL_SUCCESS;
}

int protocol_session_exit(void *arg)
{
    DLOG_ERR(LOG_PROT, "PROTOCOL(TCP): protocol_session_exit.");

    if (arg == NULL) {
        DLOG_ERR(LOG_PROT, "protocol_session_init parameters error.");
        return -E_ARGS_ILLEGAL;
    }

    return php_serialize_session_exit(arg);
}

static void php_serialize_on_unload(prot_node_t *prot)
{
    prot = 0;
}

void protocol_on_unload(prot_node_t *prot)
{
    DLOG_ERR(LOG_PROT, "PROTOCOL(TCP): protocol_on_unload.");
    return php_serialize_on_unload(prot);
}
